package com.daxilong.starchen.player.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.daxilong.starchen.player.databinding.ActivitySearchBinding;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SearchActivity extends BaseActivity {

    private ActivitySearchBinding binding;

    @Override
    protected View getLayout() {
        binding = ActivitySearchBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    public void initView() {
        //语音输入
        binding.ivInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startVoice();
            }
        });

        //开始搜索
        binding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSearche();
            }
        });
    }

    /**
     * 开始搜索
     */
    private void startSearche() {
        String text = binding.etSearch.getText().toString().trim();//阿福->
        if (!TextUtils.isEmpty(text)) {
            try {
                text = URLEncoder.encode(text, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //String url = URL.SEARCH_URL + text;
            //getDataFromNet(url);


        } else {
            Toast.makeText(this, "你没有输入您要搜索的内容", Toast.LENGTH_SHORT).show();
        }
    }

    private void startVoice() {
//        //1.创建RecognizerDialog对象
//        RecognizerDialog mDialog = new RecognizerDialog(this, new MyInitListener());
//        //2.设置accent、 language等参数
//        mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//设置中文
//        mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");//设置普通话
//        mDialog.setParameter(SpeechConstant.DOMAIN, "iat");//设置日常用语
//        //若要将UI控件用于语义理解，必须添加以下参数设置，设置之后onResult回调返回将是语义理解
//        //结果
//        // mDialog.setParameter("asr_sch", "1");
//        // mDialog.setParameter("nlp_version", "2.0");
//        //3.设置回调接口
//        mDialog.setListener(new MyRecognizerDialogListener());
//        //4.显示dialog，接收语音输入
//        mDialog.show();
    }

    private void getDataFromNet(String url) {
//        RequestParams params = new RequestParams(url);
//        x.http().get(params, new Callback.CommonCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                LogUtil.e("联网成功=="+result);
//                processData(result);
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                LogUtil.e("联网失败=="+ex.getMessage());
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//                LogUtil.e("onCancelled=="+cex.getMessage());
//            }
//
//            @Override
//            public void onFinished() {
//                LogUtil.e("onFinished==");
//            }
//        });
    }

}